<template>
  <view class="s-checkbox-group" :class="custom_class" :style="custom_style">
    <slot />
  </view>
</template>

<script>
import componentMixin from '../../mixins/componentMixin';
import getComponentOptions from '../../common/getComponentOptions';

/**
 * s-checkbox-group 复选框父组件
 * @description 复选框组件一般用于需要多个选择的场景，搭配s-checkbox使用
 * @property {*} value 当前选中项的标识符
 * @property {Boolean} disabled 是否禁用所有复选框
 * @property {String} checkedColor 所有复选框的选中状态颜色
 * @method {Function} toggleAll(checked, skipDisabled) 修改选中状态
 * @event {Function} change (value) 选中标项改变时触发
 * @example <s-checkbox-group :v-model=""></s-checkbox-group>
 */
export default {
  name: 'SCheckboxGroup',
  mixins: [componentMixin],
  options: getComponentOptions({ virtualHost: false }),
  props: {
    value: {
      type: Array,
      default: () => [],
    },
    max: [Number, String],
    disabled: Boolean,
    checkedColor: String,
  },
  created() {
    this.checkboxGroup = [];
  },
  methods: {
    toggleAll(options) {
      if (typeof options === 'boolean') {
        options = { checked: options };
      }
      options = options || {};
      this.updateValue(this.checkboxGroup.filter(checkbox => {
        if (checkbox.disabled && options.skipDisabled) return checkbox.checked;
        return options.checked ?? !checkbox.checked;
      }).map(checkbox => checkbox.name));
    },
    updateValue(names) {
      this.$emit('input', names);
      this.$emit('change', names);
    },
  },
};
</script>
